package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Set;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllAddresss", query = "select myAddress from Address myAddress"),
		@NamedQuery(name = "findAddressByName", query = "select myAddress from Address myAddress where myAddress.name = ?1"),
		@NamedQuery(name = "findAddressByNameContaining", query = "select myAddress from Address myAddress where myAddress.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findAddressByLongitude", query = "select myAddress from Address myAddress where myAddress.longitude = ?1"),
		@NamedQuery(name = "findAddressByLatitude", query = "select myAddress from Address myAddress where myAddress.latitude = ?1"),
		@NamedQuery(name = "findAddressByRegionalcode", query = "select myAddress from Address myAddress where myAddress.regionalcode = ?1"),
		@NamedQuery(name = "findAddressByRegionalcodeContaining", query = "select myAddress from Address myAddress where myAddress.regionalcode like concat('%',?1,'%')"),
		@NamedQuery(name = "findAddressByPostcode", query = "select myAddress from Address myAddress where myAddress.postcode = ?1"),
		@NamedQuery(name = "findAddressByPostcodeContaining", query = "select myAddress from Address myAddress where myAddress.postcode like concat('%',?1,'%')"),
		@NamedQuery(name = "findAddressByLevel", query = "select myAddress from Address myAddress where myAddress.level = ?1"),
		@NamedQuery(name = "findAddressByDescription", query = "select myAddress from Address myAddress where myAddress.description = ?1"),
		@NamedQuery(name = "findAddressByDescriptionContaining", query = "select myAddress from Address myAddress where myAddress.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllAddresssByParent", query = "select myAddress from Address myAddress where myAddress.parent.id = ?1"),
		@NamedQuery(name = "findAddressById", query = "select myAddress from Address myAddress where myAddress.id = ?1"),
		@NamedQuery(name = "findAddressByPrimaryKey", query = "select myAddress from Address myAddress where myAddress.id = ?1 "),
		@NamedQuery(name = "deleteAddresssByIds", query = "delete Address where id in (?1)"),
		@NamedQuery(name = "findAddresssByIds", query = "select myAddress from Address myAddress where myAddress.id in (?1)"),
		@NamedQuery(name = "findAddressIdsByParent", query = "select myAddress.id from Address myAddress where myAddress.parent.id = ?1"),
		@NamedQuery(name = "findAddressByPhyscialPrimaryKey", query = "select myAddress from Address myAddress where  myAddress.name = ?1 and myAddress.parent.id = ?2"),
		@NamedQuery(name = "countAddresssByName", query = "select count(myAddress)  from Address myAddress where myAddress.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "address")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Address")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Address implements Serializable, Comparable<Address> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "longitude", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal longitude;

	@Column(name = "latitude", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal latitude;

	@Column(name = "regionalcode")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String regionalcode;

	@Column(name = "postcode")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String postcode;

	@Column(name = "level")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Integer level;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "parent", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Address parent;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Additiveaddress> additiveaddresssForAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "parent", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Address> addresssForParent;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Detectdepartment> detectdepartmentsForAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "sourceAddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForSourceAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "discoveryAddress", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForDiscoveryAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Foodsaleaddress> foodsaleaddresssForAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Materialaddress> materialaddresssForAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Producer> producersForAddress;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "address", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Supplier> suppliersForAddress;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setLongitude(BigDecimal longitude) {
		this.longitude = longitude;
	}

	public BigDecimal getLongitude() {
		return this.longitude;
	}

	public void setLatitude(BigDecimal latitude) {
		this.latitude = latitude;
	}

	public BigDecimal getLatitude() {
		return this.latitude;
	}

	public void setRegionalcode(String regionalcode) {
		this.regionalcode = regionalcode;
	}

	public String getRegionalcode() {
		return this.regionalcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getPostcode() {
		return this.postcode;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Integer getLevel() {
		return this.level;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setParent(Address parent) {
		this.parent = parent;
	}

	public Address getParent() {
		return parent;
	}

	public void setAdditiveaddresssForAddress(Set<Additiveaddress> additiveaddresssForAddress) {
		this.additiveaddresssForAddress = additiveaddresssForAddress;
	}

	public Set<Additiveaddress> getAdditiveaddresssForAddress() {
		if (additiveaddresssForAddress == null) {
			additiveaddresssForAddress = new java.util.LinkedHashSet<Additiveaddress>();
		}
		return additiveaddresssForAddress;
	}

	public void setAddresssForParent(Set<Address> addresssForParent) {
		this.addresssForParent = addresssForParent;
	}

	public Set<Address> getAddresssForParent() {
		if (addresssForParent == null) {
			addresssForParent = new java.util.LinkedHashSet<Address>();
		}
		return addresssForParent;
	}

	public void setDetectdepartmentsForAddress(Set<Detectdepartment> detectdepartmentsForAddress) {
		this.detectdepartmentsForAddress = detectdepartmentsForAddress;
	}

	public Set<Detectdepartment> getDetectdepartmentsForAddress() {
		if (detectdepartmentsForAddress == null) {
			detectdepartmentsForAddress = new java.util.LinkedHashSet<Detectdepartment>();
		}
		return detectdepartmentsForAddress;
	}

	public void setEventsForSourceAddress(Set<Event> eventsForSourceAddress) {
		this.eventsForSourceAddress = eventsForSourceAddress;
	}

	public Set<Event> getEventsForSourceAddress() {
		if (eventsForSourceAddress == null) {
			eventsForSourceAddress = new java.util.LinkedHashSet<Event>();
		}
		return eventsForSourceAddress;
	}

	public void setEventsForDiscoveryAddress(Set<Event> eventsForDiscoveryAddress) {
		this.eventsForDiscoveryAddress = eventsForDiscoveryAddress;
	}

	public Set<Event> getEventsForDiscoveryAddress() {
		if (eventsForDiscoveryAddress == null) {
			eventsForDiscoveryAddress = new java.util.LinkedHashSet<Event>();
		}
		return eventsForDiscoveryAddress;
	}

	public void setFoodsaleaddresssForAddress(Set<Foodsaleaddress> foodsaleaddresssForAddress) {
		this.foodsaleaddresssForAddress = foodsaleaddresssForAddress;
	}

	public Set<Foodsaleaddress> getFoodsaleaddresssForAddress() {
		if (foodsaleaddresssForAddress == null) {
			foodsaleaddresssForAddress = new java.util.LinkedHashSet<Foodsaleaddress>();
		}
		return foodsaleaddresssForAddress;
	}

	public void setMaterialaddresssForAddress(Set<Materialaddress> materialaddresssForAddress) {
		this.materialaddresssForAddress = materialaddresssForAddress;
	}

	public Set<Materialaddress> getMaterialaddresssForAddress() {
		if (materialaddresssForAddress == null) {
			materialaddresssForAddress = new java.util.LinkedHashSet<Materialaddress>();
		}
		return materialaddresssForAddress;
	}

	public void setProducersForAddress(Set<Producer> producersForAddress) {
		this.producersForAddress = producersForAddress;
	}

	public Set<Producer> getProducersForAddress() {
		if (producersForAddress == null) {
			producersForAddress = new java.util.LinkedHashSet<Producer>();
		}
		return producersForAddress;
	}

	public void setSuppliersForAddress(Set<Supplier> suppliersForAddress) {
		this.suppliersForAddress = suppliersForAddress;
	}

	public Set<Supplier> getSuppliersForAddress() {
		if (suppliersForAddress == null) {
			suppliersForAddress = new java.util.LinkedHashSet<Supplier>();
		}
		return suppliersForAddress;
	}

	public void copy(Address that) {

		setId(that.getId());

		setName(that.getName());
		setLongitude(that.getLongitude());
		setLatitude(that.getLatitude());
		setRegionalcode(that.getRegionalcode());
		setPostcode(that.getPostcode());
		setLevel(that.getLevel());
		setDescription(that.getDescription());

		setParent(that.getParent());

		setAdditiveaddresssForAddress(new java.util.LinkedHashSet<Additiveaddress>(
				that.getAdditiveaddresssForAddress()));

		setAddresssForParent(
				new java.util.LinkedHashSet<Address>(that.getAddresssForParent()));

		setDetectdepartmentsForAddress(new java.util.LinkedHashSet<Detectdepartment>(
				that.getDetectdepartmentsForAddress()));

		setEventsForSourceAddress(
				new java.util.LinkedHashSet<Event>(that.getEventsForSourceAddress()));

		setEventsForDiscoveryAddress(
				new java.util.LinkedHashSet<Event>(that.getEventsForDiscoveryAddress()));

		setFoodsaleaddresssForAddress(new java.util.LinkedHashSet<Foodsaleaddress>(
				that.getFoodsaleaddresssForAddress()));

		setMaterialaddresssForAddress(new java.util.LinkedHashSet<Materialaddress>(
				that.getMaterialaddresssForAddress()));

		setProducersForAddress(
				new java.util.LinkedHashSet<Producer>(that.getProducersForAddress()));

		setSuppliersForAddress(
				new java.util.LinkedHashSet<Supplier>(that.getSuppliersForAddress()));

	}

	public Address() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("longitude=[").append(longitude).append("] ");
		buffer.append("latitude=[").append(latitude).append("] ");
		buffer.append("regionalcode=[").append(regionalcode).append("] ");
		buffer.append("postcode=[").append(postcode).append("] ");
		buffer.append("level=[").append(level).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Address))
			return false;
		Address comparedAddress = (Address) obj;
		if (!CheckPropertyEqual(this.name, comparedAddress.name)) {
			return false;
		}
		if (!CheckPropertyEqual(this.parent.id, comparedAddress.parent.id)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Address o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
